﻿using Microsoft.AspNetCore.Mvc;
using WMS.Common.WMS.Common;
using WMS.Model;
using WMS.Repository.IRepository;
using WMS.Service.IService;

namespace wmsWebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class DepartmentController : ControllerBase
    {
        private readonly IDepartmentService _departmentService;
        private readonly ILogger<DepartmentController> _logger;

        public DepartmentController(IDepartmentService departmentService, ILogger<DepartmentController> logger)
        {
            _departmentService = departmentService;
            _logger = logger;
        }

        /// <summary>
        /// 分页查询部门列表
        /// </summary>
        [HttpGet("GetDepartmentPagedListAsync")]
        public async Task<ResultApi> GetDepartmentPagedListAsync(string departmentName = null, byte? status = null, int pageIndex = 1, int pageSize = 20)
        {
            try
            {
                var request = new DepartmentQueryRequest
                {
                    DepartmentName = departmentName,
                    Status = status,
                    PageIndex = pageIndex,
                    PageSize = pageSize
                };

                var result = await _departmentService.GetDepartmentPagedListAsync(request);
                return ResultHelper.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "部门分页查询失败");
                return ResultHelper.Error("500", "查询失败");
            }
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        [HttpPost("AddDepartmentAsync")]
        public async Task<ResultApi> AddDepartmentAsync([FromBody] AddDepartmentModelDto department)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(department.DepartmentName))
                {
                    return ResultHelper.BadRequest("部门名称不能为空！");
                }

                var (success, message) = await _departmentService.AddDepartmentAsync(department.DepartmentName, department.DepartmentDesc);

                if (success)
                {
                    return ResultHelper.Success(message);
                }
                else
                {
                    return ResultHelper.Error(message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"添加部门失败 - 部门名称: {department?.DepartmentName}");
                return ResultHelper.Error("500", "添加失败");
            }
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        [HttpPost("UpdateDepartmentAsync")]
        public async Task<ResultApi> UpdateDepartmentAsync([FromBody] UpdateDepartmentModelDto department)
        {
            try
            {
                if (department.Id <= 0)
                {
                    return ResultHelper.BadRequest("请选择部门！");
                }

                if (string.IsNullOrWhiteSpace(department.DepartmentName))
                {
                    return ResultHelper.BadRequest("部门名称不能为空！");
                }

                var (success, message) = await _departmentService.UpdateDepartmentAsync(
                    department.Id, department.DepartmentName, department.DepartmentDesc,
                    department.Status, department.ParentId, department.Minister);

                if (success)
                {
                    return ResultHelper.Success(message);
                }
                else
                {
                    return ResultHelper.Error(message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"修改部门失败 - 部门ID: {department?.Id}");
                return ResultHelper.Error("500", "修改失败");
            }
        }

        /// <summary>
        /// 启用停用部门
        /// </summary>
        [HttpPost("UpdateDepartmentStatusAsync")]
        public async Task<ResultApi> UpdateDepartmentStatusAsync([FromBody] long departmentId)
        {
            try
            {
                if (departmentId <= 0)
                {
                    return ResultHelper.BadRequest("请选择部门！");
                }

                var (success, message) = await _departmentService.UpdateDepartmentStatusAsync(departmentId);

                if (success)
                {
                    return ResultHelper.Success(message);
                }
                else
                {
                    return ResultHelper.Error(message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"更新部门状态失败 - 部门ID: {departmentId}");
                return ResultHelper.Error("500", "操作失败");
            }
        }

        /// <summary>
        /// 批量启用停用部门
        /// </summary>
        [HttpPost("UpdateDepartmentsStatusAsync")]
        public async Task<ResultApi> UpdateDepartmentsStatusAsync([FromBody] List<long> ids)
        {
            try
            {
                if (ids == null || ids.Count == 0)
                {
                    return ResultHelper.BadRequest("部门ID列表不能为空！");
                }

                var (success, message) = await _departmentService.UpdateDepartmentsStatusAsync(ids);

                if (success)
                {
                    return ResultHelper.Success(message);
                }
                else
                {
                    return ResultHelper.Error(message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"批量更新部门状态失败 - 部门数量: {ids?.Count ?? 0}");
                return ResultHelper.Error("500", "操作失败");
            }
        }

        /// <summary>
        /// 获取部门下拉框
        /// </summary>
        [HttpGet("GetDepartmentDropdownBoxAsync")]
        public async Task<ResultApi> GetDepartmentDropdownBoxAsync(long departmentId)
        {
            try
            {
                var result = await _departmentService.GetDepartmentDropdownBoxAsync(departmentId);
                return ResultHelper.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取部门下拉框失败 - 排除部门ID: {departmentId}");
                return ResultHelper.Error("500", "获取失败");
            }
        }
    }
}